package com.fxkj.cloud.drms.ui.listener;

import com.fxkj.cloud.dbs.common.constants.DataType;
import com.fxkj.cloud.dbs.common.event.stream.RemoteProductCreateEvent;
import com.fxkj.cloud.drms.application.ProductApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/3/18 13:35
 */
@Component
@Slf4j
public class RemoteProductCreateListener {

    private final ProductApplicationService productApplicationService;

    public RemoteProductCreateListener(ProductApplicationService productApplicationService) {
        this.productApplicationService = productApplicationService;
    }

    @Bean
    public Consumer<RemoteProductCreateEvent> productCreate() {
        return event -> {
            UUID rootOrganizationId = event.getRootOrganizationId();
            UUID productId = event.getProductId();
            Map<String, DataType> attributeKeyDataType = event.getAttributeKeyDataType();
            log.trace("监听到远程产品创建事件,组织id:{},产品id:{}", rootOrganizationId, productId);
            productApplicationService.createProduct(rootOrganizationId, productId, attributeKeyDataType);
        };
    }
}
